package com.ruoyi.netty.protocol;

import io.netty.buffer.ByteBuf;
import lombok.Data;

/**
 * Modbus协议消息
 */
@Data
public class ModbusMessage {
    /**
     * 事务标识符
     */
    private short transactionId;
    
    /**
     * 协议标识符 (0 = Modbus)
     */
    private short protocolId = 0;
    
    /**
     * 后续字节长度
     */
    private short length;
    
    /**
     * 单元标识符 (设备ID)
     */
    private byte unitId;
    
    /**
     * 功能码
     */
    private byte functionCode;
    
    /**
     * 数据负载
     */
    private ByteBuf data;
    
    /**
     * 获取功能码的标准名称
     */
    public String getFunctionName() {
        // 使用传统switch语句，兼容Java 8
        switch (functionCode) {
            case 1:
                return "Read Coils";
            case 2:
                return "Read Discrete Inputs";
            case 3:
                return "Read Holding Registers";
            case 4:
                return "Read Input Registers";
            case 5:
                return "Write Single Coil";
            case 6:
                return "Write Single Register";
            case 15:
                return "Write Multiple Coils";
            case 16:
                return "Write Multiple Registers";
            default:
                return "Unknown Function (" + functionCode + ")";
        }
    }
} 